package abq2.jic.jic.plugin.other.other.mob.mob;

import kd.bos.dataentity.serialization.SerializationUtils;
import kd.bos.form.*;
import kd.bos.form.control.Button;
import kd.bos.form.events.CustomEventArgs;
import kd.bos.form.events.MessageBoxClosedEvent;
import kd.bos.form.plugin.AbstractMobFormPlugin;
import kd.bos.mvc.form.MobileFormView;

import java.util.EventObject;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class BookMobSyncPlugin extends AbstractMobFormPlugin {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        addClickListeners("kdec_booksync");
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        if (evt.getSource() instanceof Button) {
            switch ( ((Button)evt.getSource()).getKey() ) {
                case "kdec_booksync":
                    HashMap args = new HashMap();
                    // qr：二维码扫码框
                    // bar：条形码扫码框
                    args.put("type", "qr");
                    MobileFormView view = (MobileFormView)this.getView();
                    view.callClientAppMethod(ClientMethod.ScanQRCode, args);
                    break;
            }
        }
    }

    @Override
    public void customEvent(CustomEventArgs e) {
        String eventName = e.getEventName();
        String content = e.getEventArgs();
        String key = e.getKey();
        if (key.equals("callAppMethod")) {

            if (eventName.equals("scanQRCode")) {
                //扫码处理逻辑
                HashMap mapBook = SerializationUtils.fromJsonString(content, HashMap.class);

                String name = ((LinkedHashMap)mapBook.get("name")).get("zh_CN").toString();
                String status = mapBook.get("kdec_billstatusfield").toString();
                switch (status) {
                    case "A":
                        status = "可借阅";
                        break;
                    case "B":
                        status = "被预约";
                        break;
                    case "C":
                        status = "已借出";
                        break;
                    case "D":
                        status = "待上架";
                        break;
                }
                String author = mapBook.get("kdec_author").toString();
                String number = mapBook.get("number").toString();

                System.out.println("二维码的值是: " + content);

                ConfirmCallBackListener confirmCallBacks = new ConfirmCallBackListener("SyncBorrowConfirm", this);
                getView().showMessage("借阅书籍", "书名：" + name + "\n编号：" + number + "\n作者：" + author + "\n借阅状态：" + status,
                        MessageTypes.Default, confirmCallBacks);

               /* getView().showConfirm("书名：" + name, "编号：" + number + "\n作者：" + author + "\n借阅状态：" + status,
                        MessageBoxOptions.OK, ConfirmTypes.Default, null, null);*/
            }
        }
    }

    @Override
    public void confirmCallBack(MessageBoxClosedEvent messageBoxClosedEvent) {
        super.confirmCallBack(messageBoxClosedEvent);



    }




}
